package com.tqjc.execute.thread.aysn;

import com.tqjc.area.entity.Roller;
import com.tqjc.area.entity.SteelPlate;
import com.tqjc.execute.cache.TaskStatus;
import com.tqjc.execute.cache.entity.WorkStatus;
import com.tqjc.execute.consts.WorkConst;
import com.tqjc.execute.utils.HttpPLCUtils;
import com.tqjc.execute.work.CraneWork;
import com.tqjc.system.core.util.SpringUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;



/**
 * @Author: 86195
 * @CreateTime: 2024-05-19  16:53
 * @Description: 一键对中线程
 * @Version: 1.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
public class AysnAlignThread extends Thread{

    //天车类型
    private String craneType;

    private String host;

    private Roller roller;

    private CraneWork craneWork;


    @Override
    public void run() {
        log.info("天车：【{}】一键对中异步线程开始运行",craneType);
        //1.写入值，检查是否写入成功

        try {

            RestTemplate restTemplate = SpringUtils.getBean(RestTemplate.class);
            int alingFaultCount = 0;


            while (true){
                String alignFaultFlag = HttpPLCUtils.plcReadbc(restTemplate, host, craneType, WorkConst.ALIGN_FAULT_LABEL, false);
                alingFaultCount++;
                if("false".equalsIgnoreCase(alignFaultFlag) ){
                    break;
                }
                if(alingFaultCount >= 3){
                    throw new Exception(craneType+"天车对中功能故障中");
                }
                Thread.sleep(1000);
            }
            //获取钢板号
//            SteelPlate steelInfo = craneWork.getSteelInfo(roller);
//
//            if(steelInfo == null){
//                //如果上游获取钢板信息为空，则取页面输入的钢板信息
//                steelInfo = craneWork.getFrontPageSteelInfo();
//            }


            //首先复位钢板已搬离信息
            HttpPLCUtils.plcWritebcPlus(restTemplate,host,craneType,WorkConst.STEEL_MOVED_DB,WorkConst.DATATYPE_BOOL,"true",WorkConst.STEEL_MOVED_LABEL,false);
            Thread.sleep(1000);
            HttpPLCUtils.plcWritebcPlus(restTemplate,host,craneType,WorkConst.STEEL_MOVED_DB,WorkConst.DATATYPE_BOOL,"false",WorkConst.STEEL_MOVED_LABEL,false);


            //写钢板信息给天车
            HttpPLCUtils.plcWritebcPlus(restTemplate,host,craneType,"DB20.6.1",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","半自动标志",false);


//            HttpPLCUtils.plcWritebcPlus(restTemplate,host,craneType,"DB20.24",HttpPLCUtils.dataTypeList.get("int16"),steelInfo.getLength()+"","任务钢板长",false);
//            HttpPLCUtils.plcWritebcPlus(restTemplate,host,craneType,"DB20.26",HttpPLCUtils.dataTypeList.get("int16"),steelInfo.getWidth()+"","任务钢板宽",false);
//            HttpPLCUtils.plcWritebcPlus(restTemplate,host,craneType,"DB20.28",HttpPLCUtils.dataTypeList.get("int16"),steelInfo.getThickness()+"","任务钢板厚",false);


            HttpPLCUtils.plcWritebcPlus(restTemplate,host,craneType, WorkConst.ALIGN_DB,WorkConst.DATATYPE_BOOL,"true",WorkConst.ALIGN_LABEL,false);

            //2.写入成功后，间隔1秒。复原点位
            Thread.sleep(2000);

//            while (true){
//                //3.复位完成后，读取对中点位是否完成信号
//                String alignFinish = HttpPLCUtils.plcReadbc(restTemplate, host, craneType, WorkConst.ALIGN_FINISH_LABEL, false);
//
//                if("true".equalsIgnoreCase(alignFinish)){
//                    log.info("天车：【{}】一键对中异步线程中，对中任务完成",craneType);
//                    craneWork.setCraneInfoMsg("对中任务完成");
//                    break;
//                }
//
//                long start = System.currentTimeMillis();
//                if(System.currentTimeMillis() - start > 10*1000){
//                    log.info("天车：【{}】一键对中异步线程中，长时间对中任务未完成",craneType);
//                    craneWork.setCraneInfoMsg("对中任务长时间未完成，请重新执行");
//
//                    break;
//                }
//            }

            HttpPLCUtils.plcWritebcPlus(restTemplate,host,craneType, WorkConst.ALIGN_DB,WorkConst.DATATYPE_BOOL,"false",WorkConst.ALIGN_LABEL,false);


            log.info("天车：【{}】一键对中异步线程运行结束",craneType);
        } catch (Exception e){
            log.error("天车：【{}】一键对中异步线程运行异常",craneType);
            craneWork.setCraneInfoMsg("一键对中功能异常");

        }
        
    }
}
